package com.cowealth.platform.common.base.vo.result;

import com.cowealth.platform.common.base.constant.GlobalReturnCodeConstants;
import com.cowealth.platform.common.base.exception.GlobalException;
import com.cowealth.platform.common.base.exception.ServiceException;

import java.io.Serializable;

/**
 * 通用返回对象
 *
 * @author : skycdm
 * @version : 1.0.0
 * @date : 2020/9/10 15:55
 */
public final class CommonResult<T> implements Serializable {

    /**
     * 错误码
     *
     * @see ReturnCode#getCode()
     */
    private Integer code;

    /**
     * 错误提示，用户可阅读
     *
     * @see ReturnCode#getMessage()
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 错误明细，内部调试错误
     */
    private String detailMessage;

    /**
     * 构造请求成功的返回体
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = GlobalReturnCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.message = "";
        return result;
    }

    /**
     * 构造不包含错误明细的返回体
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(Integer code, String message) {
        return error(code, message, null);
    }

    /**
     * 构造请求错误的返回体
     * @param code
     * @param message
     * @param detailMessage
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(Integer code, String message, String detailMessage) {
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.message = message;
        result.detailMessage = detailMessage;
        return result;
    }


    public boolean judgeSuccess() {
        return GlobalReturnCodeConstants.SUCCESS.getCode().equals(code);
    }

    public boolean judgeError() {
        return !judgeSuccess();
    }


    // ========= 和 Exception 异常体系集成 =========
    /**
     * 判断是否有异常。如果有，则抛出 {@link GlobalException} 或 {@link ServiceException} 异常
     */
    public void checkError() throws GlobalException, ServiceException {
        if (judgeSuccess()) {
            return;
        }
        // 全局异常
        if (GlobalReturnCodeConstants.isMatch(code)) {
            throw new GlobalException(code, message).setDetailMessage(detailMessage);
        }
        // 业务异常
        throw new ServiceException(code, message).setDetailMessage(detailMessage);
    }

    /**
     * 业务异常转化为对应的包含业务异常编码和信息的返回体
     * @param serviceException 业务异常
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(ServiceException serviceException) {
        return error(serviceException.getCode(), serviceException.getMessage(),
                serviceException.getDetailMessage());
    }

    /**
     * 全局异常转化为对应的包含业务异常编码和信息的返回体
     * @param globalException  全局异常
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(GlobalException globalException) {
        return error(globalException.getCode(), globalException.getMessage(),
                globalException.getDetailMessage());
    }



    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getDetailMessage() {
        return detailMessage;
    }

    public CommonResult<T> setDetailMessage(String detailMessage) {
        this.detailMessage = detailMessage;
        return this;
    }

    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                ", detailMessage='" + detailMessage + '\'' +
                '}';
    }

}
